package org.fmod;

import android.media.*;
import java.nio.*;
import android.util.*;

public class FMODAudioDevice implements Runnable
{
    private static int h = 0;
    private static int i = 1;
    private static int j = 2;
    private static int k = 3;
    private volatile Thread a;
    private volatile boolean b;
    private AudioTrack c;
    private boolean d;
    private ByteBuffer e;
    private byte[] f;
    private volatile a g;

    public FMODAudioDevice() {
        this.a = null;
        this.b = false;
        this.c = null;
        this.d = false;
        this.e = null;
        this.f = null;
    }

    private native int fmodGetInfo(final int p0);

    private native int fmodProcess(final ByteBuffer p0);

    private void releaseAudioTrack() {
        final AudioTrack c = this.c;
        if (c != null) {
            if (c.getState() == 1) {
                this.c.stop();
            }
            this.c.release();
            this.c = null;
        }
        this.e = null;
        this.f = null;
        this.d = false;
    }

    public void close() {
        synchronized (this) {
            this.stop();
        }
    }

    native int fmodProcessMicData(final ByteBuffer p0, final int p1);

    public boolean isRunning() {
        return this.a != null && this.a.isAlive();
    }

    @Override
    public void run() {
        int n = 3;
        while (this.b) {
            int n2 = n;
            if (!this.d && (n2 = n) > 0) {
                this.releaseAudioTrack();
                final int fmodGetInfo = this.fmodGetInfo(FMODAudioDevice.h);
                int n3 = Math.round(AudioTrack.getMinBufferSize(fmodGetInfo, 3, 2) * 1.1f) & 0xFFFFFFFC;
                final int fmodGetInfo2 = this.fmodGetInfo(FMODAudioDevice.i);
                final int n4 = this.fmodGetInfo(FMODAudioDevice.j) * fmodGetInfo2 * 4;
                if (n4 > n3) {
                    n3 = n4;
                }
                this.c = new AudioTrack(3, fmodGetInfo, 3, 2, n3, 1);
                this.d = (this.c.getState() == 1);
                if (this.d) {
                    this.e = ByteBuffer.allocateDirect(fmodGetInfo2 * 2 * 2);
                    this.f = new byte[this.e.capacity()];
                    this.c.play();
                    n2 = 3;
                }
                else {
                    final StringBuilder sb = new StringBuilder("AudioTrack failed to initialize (status ");
                    sb.append(this.c.getState());
                    sb.append(")");
                    Log.e("FMOD", sb.toString());
                    this.releaseAudioTrack();
                    n2 = n - 1;
                }
            }
            n = n2;
            if (this.d) {
                if (this.fmodGetInfo(FMODAudioDevice.k) == 1) {
                    this.fmodProcess(this.e);
                    final ByteBuffer e = this.e;
                    e.get(this.f, 0, e.capacity());
                    this.c.write(this.f, 0, this.e.capacity());
                    this.e.position(0);
                    n = n2;
                }
                else {
                    this.releaseAudioTrack();
                    n = n2;
                }
            }
        }
        this.releaseAudioTrack();
    }

    public void start() {
        synchronized (this) {
            if (this.a != null) {
                this.stop();
            }
            (this.a = new Thread(this, "FMODAudioDevice")).setPriority(10);
            this.b = true;
            this.a.start();
            if (this.g != null) {
                this.g.b();
            }
        }
    }

    public int startAudioRecord(int a, final int n, final int n2) {
        synchronized (this) {
            if (this.g == null) {
                (this.g = new a(this, a, n)).b();
            }
            a = this.g.a();
            return a;
        }
    }

    public void stop() {
        //
        // This method could not be decompiled.
        //
        // Original Bytecode:
        //
        //     1: monitorenter
        //     2: aload_0
        //     3: getfield        org/fmod/FMODAudioDevice.a:Ljava/lang/Thread;
        //     6: ifnull          29
        //     9: aload_0
        //    10: iconst_0
        //    11: putfield        org/fmod/FMODAudioDevice.b:Z
        //    14: aload_0
        //    15: getfield        org/fmod/FMODAudioDevice.a:Ljava/lang/Thread;
        //    18: invokevirtual   java/lang/Thread.join:()V
        //    21: aload_0
        //    22: aconst_null
        //    23: putfield        org/fmod/FMODAudioDevice.a:Ljava/lang/Thread;
        //    26: goto            2
        //    29: aload_0
        //    30: getfield        org/fmod/FMODAudioDevice.g:Lorg/fmod/a;
        //    33: ifnull          43
        //    36: aload_0
        //    37: getfield        org/fmod/FMODAudioDevice.g:Lorg/fmod/a;
        //    40: invokevirtual   org/fmod/a.c:()V
        //    43: aload_0
        //    44: monitorexit
        //    45: return
        //    46: astore_1
        //    47: aload_0
        //    48: monitorexit
        //    49: goto            54
        //    52: aload_1
        //    53: athrow
        //    54: goto            52
        //    57: astore_1
        //    58: goto            2
        //    Exceptions:
        //  Try           Handler
        //  Start  End    Start  End    Type
        //  -----  -----  -----  -----  --------------------------------
        //  2      14     46     57     Any
        //  14     26     57     61     Ljava/lang/InterruptedException;
        //  14     26     46     57     Any
        //  29     43     46     57     Any
        //
        // The error that occurred was:
        //
        // java.lang.IllegalStateException: Expression is linked from several locations: Label_0029:
        //     at com.strobel.decompiler.ast.Error.expressionLinkedFromMultipleLocations(Error.java:27)
        //     at com.strobel.decompiler.ast.AstOptimizer.mergeDisparateObjectInitializations(AstOptimizer.java:2596)
        //     at com.strobel.decompiler.ast.AstOptimizer.optimize(AstOptimizer.java:235)
        //     at com.strobel.decompiler.ast.AstOptimizer.optimize(AstOptimizer.java:42)
        //     at com.strobel.decompiler.languages.java.ast.AstMethodBodyBuilder.createMethodBody(AstMethodBodyBuilder.java:214)
        //     at com.strobel.decompiler.languages.java.ast.AstMethodBodyBuilder.createMethodBody(AstMethodBodyBuilder.java:99)
        //     at com.strobel.decompiler.languages.java.ast.AstBuilder.createMethodBody(AstBuilder.java:782)
        //     at com.strobel.decompiler.languages.java.ast.AstBuilder.createMethod(AstBuilder.java:675)
        //     at com.strobel.decompiler.languages.java.ast.AstBuilder.addTypeMembers(AstBuilder.java:552)
        //     at com.strobel.decompiler.languages.java.ast.AstBuilder.createTypeCore(AstBuilder.java:519)
        //     at com.strobel.decompiler.languages.java.ast.AstBuilder.createTypeNoCache(AstBuilder.java:161)
        //     at com.strobel.decompiler.languages.java.ast.AstBuilder.createType(AstBuilder.java:150)
        //     at com.strobel.decompiler.languages.java.ast.AstBuilder.addType(AstBuilder.java:125)
        //     at com.strobel.decompiler.languages.java.JavaLanguage.buildAst(JavaLanguage.java:71)
        //     at com.strobel.decompiler.languages.java.JavaLanguage.decompileType(JavaLanguage.java:59)
        //     at us.deathmarine.luyten.DecompilerLinkProvider.generateContent(DecompilerLinkProvider.java:97)
        //     at us.deathmarine.luyten.OpenFile.decompileWithNavigationLinks(OpenFile.java:494)
        //     at us.deathmarine.luyten.OpenFile.decompile(OpenFile.java:467)
        //     at us.deathmarine.luyten.Model.extractClassToTextPane(Model.java:420)
        //     at us.deathmarine.luyten.Model.openEntryByTreePath(Model.java:339)
        //     at us.deathmarine.luyten.Model$TreeListener$1.run(Model.java:266)
        //
        throw new IllegalStateException("An error occurred while decompiling this method.");
    }

    public void stopAudioRecord() {
        synchronized (this) {
            if (this.g != null) {
                this.g.c();
                this.g = null;
            }
        }
    }
}
